package cn.bdqn.controller;

import cn.bdqn.domain.Registration;
import cn.bdqn.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 挂号表controller
 */
@RestController
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    /**
     * 接收小程序传来的值，添加到数据库
     * @param idCard 身份证号
     * @param departmentId 科室
     * @param userName 用户名
     * @param phone 手机号
     * @param id    用户id
     * @param type  挂号类型
     * @param haoduan   号段类型 传来的是上午/下午
     * @param money     金额
     * @param time      预约时间
     * @param openId    微信小程序openId
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Map<String,Object> insertRegistration(String idCard, Integer departmentId, String userName, String phone, Integer id, String type, String haoduan, Double money, String time, String openId,String keshi){
        System.out.println(idCard+"科室id"+departmentId+userName+phone+id+type+haoduan+money+time+openId+keshi);

        Integer isFa = 0;
        if("上午".equals(haoduan)){
            isFa = 0;
        }else if ("下午".equals(haoduan)){
            isFa = 1;
        }
        //将String类型的日期转为data类型
        Date date = new Date();

        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");

        try{
            date = ft.parse(time);
        }catch (Exception e){
            e.printStackTrace();
        }
        Registration registration = new Registration(idCard,userName,phone,isFa,id,date,departmentId,money,type,openId,keshi);

        int result = registrationService.insertRegistration(registration);

        Map<String,Object> map = new HashMap<>();

        map.put("insertStatus",result);

        return  map;
    }

    /**
     * 根据openId查询用户挂号信息
     * @param openId
     * @return
     */
    @RequestMapping("/queryRegInfo")
    @ResponseBody
    public Map<String,Object> queryRegInfo(String openId){

        Map<String,Object> map = new HashMap<>();
        try {
            List<Registration> regList = registrationService.selectRegistrations(openId);
            map.put("regList",regList);
            System.out.println(regList);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","异常");
            return map;
        }
    }

}
